import {
  selectAllPollenClass,
  selectPollenClass
} from 'services/pollenClass';
/*
* @author JinHang
* @description 废品查价的数据管理
* @date 2018/6/22 10:04
* */
// const cityNo = 210100000000;
const price = {
  state: {
    wasteGroups: [],// 废品类集合，应该是API请求的结果
    wasteList: [],// 选择某一类后的废品列表
    wasteForSaleList: []// 将要卖的废品列表
  },
  mutations: {
    SET_WASTE_GROUPS: (state, wasteGroups) => {
      // getWasteGroups获取数据
      state.wasteGroups = wasteGroups;
    },
    SET_WASTE_LIST: (state, index) => {
      // 用户点击废品类tab后取出改类下的废品列表
      state.wasteList = state.wasteGroups[index].pollenSubjects;

      if (state.wasteForSaleList.length > 0) {
        state.wasteForSaleList.forEach((saleWaste, saleWasteIndex) => {
          state.wasteList.forEach((waste, wasteIndex) => {
            if (saleWaste.identityNo === waste.identityNo) {
              waste.isAdd = true;
            }
          });
        });
      }
    },
    ADD_WASTE_TO_SALE_LIST: (state, waste) => {
      // 将废品加入将要卖的列表
      state.wasteForSaleList.push(waste);
      // 将当前点击的废品标记为已加入
      waste.isAdd = true;
    },
    DEL_WASTE_FORM_SALE_LIST: (state, waste) => {

      const temp = [];

      state.wasteList.forEach((wasteItem, wasteIndex) => {
        if (wasteItem.identityNo === waste.identityNo) {
          wasteItem.isAdd = false;
        }
      });

      state.wasteForSaleList.forEach((saleWaste, saleWasteIndex) => {
        if (waste.identityNo !== saleWaste.identityNo) {
          temp.push(saleWaste);
        }
      });

      state.wasteForSaleList = temp;

    },
    EMPTY_WASTE_FOR_SALE_LIST: (state) => {

      state.wasteForSaleList = [];

      // 直接将所有废品列表重置为未选择
      state.wasteList.forEach((waste, index) => {
        waste.isAdd = false;
      })

    }
  },
  actions: {
    /*
    * @author JinHang
    * @description 获取废品类集合
    * @date 2018/6/22 10:11
    * */
    getWasteGroups({ commit }, cityNo ) {

      return new Promise((resolve, reject) => {

        selectPollenClass({ cityNo }).then((response) => {

          commit('SET_WASTE_GROUPS', response.object);

          resolve(response);

        }).catch((e) => {

          reject(e);

        });

      });
    },
    /*
    * @author JinHang
    * @description 选择某一类取出该类下的废品列表
    * @param { number } index 选择的类的索引
    * @date 2018/6/22 10:11
    * */
    setWasteList({ commit }, index) {
      commit('SET_WASTE_LIST', index);
    },
    /*
    * @author JinHang
    * @description 将一个废品加入将要卖的列表
    * @param { object } waste 是选择的废品
    * @date 2018/6/22 10:12
    * */
    addWasteToSaleList({ commit }, waste) {
      commit('ADD_WASTE_TO_SALE_LIST', waste);
    },
    /*
    * @author JinHang
    * @description 将一个废品从将要卖的列表中去除
    * @param { object } waste 是要去除的废品
    * @date 2018/6/22 10:13
    * */
    delWasteFromSaleList({ commit }, waste) {
      commit('DEL_WASTE_FORM_SALE_LIST', waste);
    },
    /*
    * @author JinHang
    * @description 清空将要卖的列表
    * @date 2018/6/22 10:14
    * */
    emptyWasteForSaleList({ commit }) {
      commit('EMPTY_WASTE_FOR_SALE_LIST');
    }
  }
};
export default price;
